package com.healthtop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.healthtop.common.Page;
import com.healthtop.dto.ResourceDto;
import com.healthtop.dto.SitesDto;
import com.healthtop.mapper.DIYMapper;
import com.healthtop.model.*;
import com.healthtop.service.DIYService;
import com.healthtop.service.XsArticleCategoryService;
import com.healthtop.service.XsDictionaryService;
import com.healthtop.utils.AliyunOSSClientUtil;
import com.healthtop.utils.ImageUploadUtil;
import com.healthtop.utils.StringUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class DIYServiceImpl implements DIYService {

    @Autowired
    private DIYMapper diyMapper;

    @Autowired
    private XsDictionaryService xsDictionaryService;

    @Autowired
    private XsArticleCategoryService xsArticleCategoryService;

    @Override
    public List<ResourceDto> getParentResource(long uid) {
        return diyMapper.getParentResource(uid);
    }

    @Override
    public List<ResourceDto> getResourceByPid(long id, long uid) {
        return diyMapper.getResourceByPid(id,uid);
    }

    @Override
    public Page selectXsGameGategoryPage(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsGameGategoryList(page);
        int total = diyMapper.selectXsGameGategoryListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public Page selectXsGameDetailsPage(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsGameDetailsList(page);
        int total = diyMapper.selectXsGameDetailsListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public List<XsUser>  selectUser(String userName) {
        return diyMapper.selectXsUser(userName);
    }

    @Override
    public List<XsRole> selectXsRoleByUid(Long userId) {
        return diyMapper.selectXsRoleByUid(userId);
    }

    @Override
    public int selectXsUserCount(Map map) {
        return diyMapper.selectXsUserCount(map);
    }

    @Override
    public Page getMenuList(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsMenuList(page);
        int total = diyMapper.selectXsMenuListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public Page selectXsRoleList(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsRoleList(page);
        int total = diyMapper.selectXsRoleListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public List<Long> selectMenuIdByRoleId(long roleId) {
        return diyMapper.selectMenuIdByRoleId(roleId);
    }

    @Override
    public Page selectXsUserList(Page page) {
        if(!ObjectUtils.isEmpty(page.getKeyword())){
            page.setKeyword(page.getKeyword().trim());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsUserList(page);
        int total = diyMapper.selectXsUserListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public SitesDto getSitesAll(HttpServletRequest request, HttpServletResponse response, SitesDto sitesDto, Page page) {
        if(ObjectUtils.isEmpty(request.getSession().getAttribute("xsMenu"))){
            List<Map> cmsXsMenuList = diyMapper.getCmsXsMenuList();
            request.getSession().setAttribute("xsMenu",cmsXsMenuList);
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        List<Map> gameList =  diyMapper.getGameGategoryList(page);
        int total = diyMapper.getGameGategoryListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(gameList);
        page.setPageIndex(total,page.getNowpage());
        sitesDto.setPage(page);
        return sitesDto;
    }

    @Override
    public List<Map> getCmsXsMenuList() {
        return diyMapper.getCmsXsMenuList();
    }

    @Override
    public Page selectXsDictionaryList(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsDictionaryList(page);
        int total = diyMapper.selectXsDictionaryListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }

    @Override
    public SitesDto forwardGameDetail(SitesDto sitesDto,Page page) {
        Map map = Maps.newHashMap();
        List<XsArticleCategory> dictionaryList =  xsArticleCategoryService.selectByGameId(sitesDto.getGameId());
        if(ObjectUtils.isEmpty(sitesDto.getArticleCategory()) && dictionaryList.size()>0){
            sitesDto.setArticleCategory(dictionaryList.get(0).getId());
        }
        map.put("startindex",(page.getNowpage()-1)*page.getPageSize());
        map.put("pageSize",page.getPageSize());
        map.put("keyword",page.getKeyword());
        map.put("gameId",sitesDto.getGameId());
        map.put("articleCategory",sitesDto.getArticleCategory());
        List<Map> gameList =  diyMapper.forwardGameDetail(map);
        int total = diyMapper.forwardGameDetailCount(map);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(gameList);
        page.setPageIndex(total,page.getNowpage());
        sitesDto.setDictionaryList(dictionaryList);
        sitesDto.setPage(page);
        return sitesDto;
    }

    @Override
    public String upload(HttpServletRequest request, HttpServletResponse response) {
        String DirectoryName = "uploads";
        Map ckEditorImg = null;
        try {
            ckEditorImg = ImageUploadUtil.ckeditor(request, response, DirectoryName);
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(ckEditorImg);
    }

    @Override
    public String videoUpload(HttpServletRequest request, HttpServletResponse response) {
        String DirectoryName = "uploads";
        Map ckEditorVideo = null;
        try {
            ckEditorVideo = ImageUploadUtil.ckeditorVideo(request, response, DirectoryName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JSONObject.toJSONString(ckEditorVideo);
    }

    @Override
    public Page selectXsArticleCategoryPage(Page page) {
        if(!org.springframework.util.StringUtils.isEmpty(page.getKeyword())) {
            page.setKeyword(page.getKeyword());
        }
        page.setStartindex((page.getNowpage()-1)*page.getPageSize());
        page.setPageSize(page.getPageSize());
        List list = diyMapper.selectXsArticleCategoryList(page);
        int total = diyMapper.selectXsArticleCategoryListCount(page);
        page.setCountpage(page.getCountpage());
        page.setCountrecord(total);
        page.setAllentities(list);
        page.setPageIndex(total,page.getNowpage());
        return page;
    }
}
